﻿using UnityEngine;
using System.Collections;

public class AnimatorCtrl : MonoBehaviour {
	[SerializeField] HumanBodyBones bone;
	Animator anim;
	[SerializeField] Vector3 rotateSpeed = new Vector3(45f, 0f, 0f);
	[SerializeField] AnimationCurve curve;
	Quaternion quat = Quaternion.Euler(0f,0f,0f);
	[SerializeField] float offset = 0f;
	float timer = 0f;
	[SerializeField] float amplifier = 100f;
	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
	}
	//函数被调用前提
	//1.Animator的Layers界面的设置中要勾选IKPass
	//2.Rig中的Type要设为Humanoid
	void OnAnimatorIK (int layerIndex) {
		//quat *= Quaternion.Euler (rotateSpeed * Time.deltaTime);
		//anim.SetBoneLocalRotation (bone, quat);

		float period = curve [curve.length-1].time;
		timer += Time.deltaTime;
		timer = timer > period ? 0f : timer;
		bone = HumanBodyBones.LeftThumbProximal;
		while (bone != HumanBodyBones.RightThumbProximal) {
			anim.SetBoneLocalRotation (bone, anim.GetBoneTransform(bone).localRotation * Quaternion.Euler (curve.Evaluate(timer) * amplifier, 0f, 0f));
			bone++;
		}
	}
}
